﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using OgilvyOne.Model;
using OgilvyOne.DALFactory;
using OgilvyOne.Utility;
using System.Runtime.Remoting.Contexts;
using System.Configuration;
using System.Web;


namespace OgilvyOne.BLL
{
    /// <summary>
    /// 前台栏目(菜单)解析类

    /// </summary>
    public class MenuModel
    {

        public static string DEBUG = System.Configuration.ConfigurationSettings.AppSettings["Debug"];

        //private const int CN_HOME_ID = ConConstants.HOME_ID;
        private int cid; //当前ID
        private int aid; //当前内容
        private static IList columnlist; //所有栏目

        private static Hashtable columnmap; //栏目map
        private string rootDir;

        public MenuModel()
        {
            cid = GetHomeId();
            init();
        }
        public MenuModel(int cid, int aid)
        {
            this.cid = cid;
            this.aid = aid;
            init();
        }
        public MenuModel(int cid)
        {
            this.cid = cid;
            init();
        }

        protected virtual int GetHomeId()
        {
            return ConConstants.HOME_ID;
        }
        /// <summary>
        /// 根目录
        /// </summary>
        /// <returns></returns>
        protected string GetRootDir()
        {
            return "/" + rootDir;
        }

        public void SetCid(int cid)
        {
            this.cid = cid;
        }
        /// <summary>
        /// 栏目主菜单ID
        /// </summary>
        /// <param name="cid"></param>
        /// <returns></returns>
        public int GetMainColumnId(int cid)
        {
            string layerid = GetColumn(cid).Layerid;
            string[] ls = layerid.Split(',');
            return int.Parse(ls[2]);
        }
        /// <summary>
        /// 当前栏目主菜单ID
        /// </summary>
        /// <returns></returns>
        public int GetMainColumnId()
        {
            return GetMainColumnId(cid);
        }


        public void SetAid(int aid)
        {
            this.aid = aid;
        }
        //初始栏目数据
        private void init()
        {
            bool isupdate = ColumnBll.IsUpdate();
            if (columnlist == null || isupdate)
            {
                columnlist = ColumnBll.GetColumnList();
            }
            if (columnmap == null || isupdate)
            {
                columnmap = new Hashtable();
                for (int i = 0; i < columnlist.Count; i++)
                {
                    ColumnModel cm = (ColumnModel)columnlist[i];
                    columnmap.Add(cm.Classid, cm);
                }
            }
            rootDir = GetColumn(GetHomeId()).Eclassname;
        }

        public bool IsDebug()
        {
            if (DEBUG != null && DEBUG == "1")
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 返回当前栏目信息
        /// </summary>
        /// <returns></returns>
        public ColumnModel GetColumn()
        {
            return GetColumn(cid);
        }
        /// <summary>
        /// 返回栏目信息
        /// </summary>
        /// <param name="_cid"></param>
        /// <returns></returns>
        public ColumnModel GetColumn(int _cid)
        {
            return (ColumnModel)columnmap[_cid];
        }
        /// <summary>
        /// 获取子栏目

        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public IList GetSonColumn(int pid)
        {
            IList datalist = new ArrayList();
            for (int i = 0; i < columnlist.Count; i++)
            {
                ColumnModel cm = (ColumnModel)columnlist[i];
                if (cm.Parentid == pid)
                {
                    datalist.Add(cm);
                }
            }
            return datalist;
        }
        /// <summary>
        /// 获取导航子栏目
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public IList GetNavSonColumn(int pid)
        {
            IList datalist = new ArrayList();
            for (int i = 0; i < columnlist.Count; i++)
            {
                ColumnModel cm = (ColumnModel)columnlist[i];
                if (cm.Parentid == pid && cm.Isshow == 1) //并且导航
                {
                    datalist.Add(cm);
                }
            }
            return datalist;
        }

        /// <summary>
        /// 获取层级所有子栏目
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public IList GetLayeridColumn(int pid)
        {
            IList datalist = new ArrayList();
            ColumnModel pcm = GetColumn(pid);
            string layerid = pcm.Layerid;
            for (int i = 0; i < columnlist.Count; i++)
            {
                ColumnModel cm = (ColumnModel)columnlist[i];
                if (cm.Classid != pid && cm.Layerid.IndexOf(layerid) > -1)
                {
                    datalist.Add(cm);
                }
            }
            return datalist;
        }

        public string GetLayerDir(string layerid)
        {
            string dir = "";
            string[] cs = layerid.Split(',');
            foreach (string _cid in cs)
            {
                int _cidi = int.Parse(_cid);
                if (_cidi >= GetHomeId())
                {
                    ColumnModel cm = GetColumn(_cidi);
                    if (cm!=null && cm.Eclassname != null && cm.Eclassname.Trim() != "")
                    {
                        dir += "/" + cm.Eclassname;
                    }
                }
            }
            return dir;
        }

        /// <summary>
        /// 栏目Url
        /// </summary>
        /// <param name="cid"></param>
        /// <returns></returns>
        public string GetUrl(int cid)
        {
            return GetUrl(GetColumn(cid));
        }
        public string GetUrl(ColumnModel cm)
        {
            if (cm.Templateindex == 0 && !(cm.Columnfilename != null && cm.Columnfilename != ""))
            {
                return "javascript:;";
            }
            else if (cm.Columnfilename != null && cm.Columnfilename != "")
            {
                if (IsDebug() == false || cm.Columnfilename.IndexOf(".aspx") > 0)
                {
                    if (cm.Columnfilename.IndexOf("http://") > -1)
                    {
                        return "javascript:window.open('"+cm.Columnfilename+"','_blank')";
                    }
                    if (cm.Columnfilename.IndexOf("/") > -1)
                    {
                        return cm.Columnfilename;
                    }
                    string dir = GetLayerDir(cm.Layerid);
                    return dir + "/" + cm.Columnfilename;
                }
            }
            else if (cm.Class_is_static == CmsConstants.YES)
            {
                if (IsDebug() == false)
                {
                    string dir = GetLayerDir(cm.Layerid);
                    return dir + "/" + GetColumnHtmlFileName(cm);
                }
            }
            return GetRootDir() + "/content.aspx?cid=" + cm.Classid;
        }
        public static string GetColumnHtmlFileName(ColumnModel cm)
        {
            return cm.Columnfilename != null && cm.Columnfilename != "" ? cm.Columnfilename : "c-" + cm.Classid + ".html";
        }
        public static string GetAritcleHtmlFileName(CmsArticleModel cm)
        {
            return cm.Staticpagename != null && cm.Staticpagename.Trim() != "" ? cm.Staticpagename : cm.Articleid + ".html";
        }
        public string GetPageUrl(int cid, int pagenum)
        {
            return GetRootDir() + "/content.aspx?cid=" + cid + "&pagenum=" + pagenum;
        }
        /// <summary>
        /// 文章URL
        /// </summary>
        /// <param name="cm"></param>
        /// <returns></returns>
        public string GetArticleUrl(CmsArticleModel cm)
        {

            if (cm.Articletype == OgilvyOne.Utility.CmsConstants.ARTICLE_TYPE_DOWNLOAD)
            {
                return ConConstants.UPLOADFILES_FILE_PATH + cm.Linkpath;
            }
            else if (cm.Articletype == OgilvyOne.Utility.CmsConstants.ARTICLE_TYPE_LINK)
            {
                return cm.Linkpath; //"javascript:window.open('"+ cm.Linkpath + "','_blank');"
            }
            if (cm.Isstaticpub == CmsConstants.YES.ToString())
            {
                if (IsDebug() == false)
                {
                    string dir = GetLayerDir(GetColumn(cm.Columnid).Layerid);
                    return dir + "/" + GetAritcleHtmlFileName(cm);
                }
            }
            return GetRootDir() + "/content.aspx?aid=" + cm.Articleid;
        }

        
        //public string GetFileDownloadUrl(CmsArticleModel cm);

        public string GetLeftNavi(int _mainid)
        {
            ColumnModel _dcm = GetColumn(); //当前
            StringBuilder sb = new StringBuilder();
            return sb.ToString();
        }

        

        //当前位置
        public string GetPositionNavi()
        {
            StringBuilder sb = new StringBuilder();
            
            return sb.ToString();
        }

        public string GetMenuListTitle()
        {
            string[] cs = GetColumn().Layerid.Split(',');
            StringBuilder sb = new StringBuilder();
            if (cs.Length > 2)
            {
                for (int i = cs.Length - 2; i > 0; i--)
                {
                    if (cs[i] == GetHomeId().ToString())
                    {
                        break;
                    }
                    sb.Append(" - ").Append(GetColumn(int.Parse(cs[i])).Classname);
                }
            }
            return sb.ToString();
        }
        /// <summary>
        /// 栏目KV图
        /// </summary>
        /// <returns></returns>
        public string GetColumntTitleImg()
        {
            string classpic = GetColumn().Classpic;
            if (classpic == null || classpic.Trim() == "")
            {
                string[] cs = GetColumn().Layerid.Split(',');
                if (cs.Length > 2)
                {
                    for (int i = cs.Length - 2; i > 0; i--)
                    {
                        classpic = GetColumn(int.Parse(cs[i])).Classpic;
                        if (classpic != null && classpic.Trim() != "")
                        {
                            break;
                        }
                        if (cs[i] == GetHomeId().ToString())
                        {
                            break;
                        }
                    }
                }
            }

            return ConConstants.UPLOADFILES_IMAGE_PATH + classpic;
        }

        /// <summary>
        /// 栏目SEO
        /// </summary>
        /// <returns></returns>
        public string GetColumntSEO()
        {
            ColumnModel cm = GetColumn();
            string keyword = cm.Columnkeyword;
            string desc = cm.Columndesc;
            if (keyword == null || keyword.Trim() == "")
            {
                string[] cs = cm.Layerid.Split(',');
                if (cs.Length > 2)
                {
                    for (int i = cs.Length - 2; i > 0; i--)
                    {
                        ColumnModel temcm = GetColumn(int.Parse(cs[i]));
                        keyword = temcm.Columnkeyword;
                        desc = temcm.Columndesc;

                        if (keyword != null && keyword.Trim() != "")
                        {
                            break;
                        }
                        if (cs[i] == GetHomeId().ToString())
                        {
                            break;
                        }
                    }
                }
            }
            StringBuilder sb = new StringBuilder();
            sb.Append("<meta name=\"Keywords\" content=\"");
            sb.Append(keyword);
            sb.Append("\" />\n");

            sb.Append("<meta name=\"Description\" content=\"");
            sb.Append(desc);
            sb.Append("\" />\n");
            sb.Append(CodeMeta());
            return sb.ToString();
        }
        /// <summary>
        /// 添加监测代码Meta
        /// </summary>
        /// <returns></returns>
        public string CodeMeta()
        {
            StringBuilder sb = new StringBuilder();
            
            return sb.ToString();
        }

        private string GetColumnCodeMeta(ColumnModel cm)
        {
            return cm != null && cm.Code_meta != null && cm.Code_meta != "" ? cm.Code_meta + "\n" : "";
        }
        
    }
}
